$(function () {


    $(".nav-content").each(function () {
        $(this).click(function () {
            $(".nav-content").removeClass("bg-secondary text-light")
            $(this).addClass("bg-secondary text-light")
            
        });
    });

    $("#my_blog").click(function(){
        $.ajax({
            url: "/manage-blog/",
            type: "GET",
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串;
                $("#content").empty();
                var topics = data.topics;
                var str_s = '<table class="table">'+
                        '<thead>'+
                        '<tr>'+
                            '<th>序号</th>'+
                            '<th>帖子名称</th>'+
                            '<th>帖子简介</th>'+
                        '</tr>'+
                        '</thead>'+
                        '<tbody>';
                var str_t = '';
                for(var i=0;i<topics.length;i++){
                                str_t +=
                                '<tr>'+
                                '<td>'+ topics[i].id +'</td>'+
                                '<td>'+ topics[i].title +'</td>'+
                                '<td>'+ topics[i].introduce +'</td>'+
                                '<td>'+
                                    '<button style="float: right;" class="btn" iddd="'+ topics[i].id +'" onclick="deletet(this);">删除</button>'+
                                    '<a href="/single/'+ topics[i].id +'"><button style="float: right;" class="btn">查看</button></a>'+
                                '</td>'+
                            '</tr>'

                            }           
                var str_e =
                        '</tbody>'+
                    '</table>';

                $("#content").html(str_s+str_t+str_e);
            }
        });
    });

    // 我的好友
    $("#my_friends").click(function(){
        $.ajax({
            url: "/my-friends/",
            type: "GET",
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串;
                $("#content").empty();
                var friends = data.friends
                var str_s = '<table class="table">'+
                '<thead>'+
                  '<tr>'+
                    '<th scope="col">ID</th>'+
                    '<th scope="col">用户名</th>'+
                    '<th scope="col">学院</th>'+
                    '<th scope="col">专业</th>'+
                    '<th scope="col">性别</th>'+
                    '<th scope="col">年龄</th>'+
                    '<th scope="col">个人简介</th>'+
                  '</tr>'+
                '</thead>'+
                '<tbody>'
                var str_t =''
                for(var i=0;i<friends.length;i++){
                    str_t +=
                        '<tr>'+
                            '<th scope="row">'+ friends[i].id +'</th>'+
                            '<td>'+ friends[i].username +'</td>'+
                            '<td>'+ friends[i].faculty +'</td>'+
                            '<td>'+ friends[i].major +'</td>'+
                            '<td>'+ friends[i].sex +'</td>'+
                            '<td>'+ friends[i].age +'</td>'+
                            '<td>'+ friends[i].profile +'</td>'+
                            '<td other_id="'+ friends[i].id +'" onclick="deletet_friend(this);">删除</td>'+
                        '</tr>'
                }

                var str_e = 
                '</tbody>'+
              '</table>'
              $("#content").html(str_s+str_t+str_e);
                
            }
        });
    });


    // 好友申请
    $("#my_request").click(function(){
        $.ajax({
            url: "/get-request/",
            type: "GET",
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串;
                $("#content").empty();
                var friends = data.friends
                console.log(friends)
                var str_s = '<table class="table">'+
                '<thead>'+
                  '<tr>'+
                    '<th scope="col">ID</th>'+
                    '<th scope="col">用户名</th>'+
                    '<th scope="col">学院</th>'+
                    '<th scope="col">专业</th>'+
                    '<th scope="col">性别</th>'+
                    '<th scope="col">年龄</th>'+
                    '<th scope="col">个人简介</th>'+
                  '</tr>'+
                '</thead>'+
                '<tbody>'
                var str_t =''
                for(var i=0;i<friends.length;i++){
                    str_t +=
                        '<tr>'+
                            '<th scope="row">'+ friends[i].id +'</th>'+
                            '<td>'+ friends[i].username +'</td>'+
                            '<td>'+ friends[i].faculty +'</td>'+
                            '<td>'+ friends[i].major +'</td>'+
                            '<td>'+ friends[i].sex +'</td>'+
                            '<td>'+ friends[i].age +'</td>'+
                            '<td>'+ friends[i].profile +'</td>'+
                            '<td onclick="agree(this);" other_id="'+ friends[i].id +'">同意</td>'+
                            '<td  onclick="refusal(this);" other_id="'+ friends[i].id +'">拒绝</td>'+ 
                        '</tr>'
                }

                var str_e = 
                '</tbody>'+
              '</table>'
              $("#content").html(str_s+str_t+str_e);
                
            }
        });
    });
    
    // 添加好友
    $("#add_friend").click(function(){
        $.ajax({
            url: "/add-friend/",
            type: "GET",
            beforeSend: function (xhr, settings) {
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
            },
            success: function (data) {
                // data是服务器端返回的字符串;
                $("#content").empty();     
                var str_s='<p>'+
                                '<input id="username" class="mt-3 " type="text" placeholder="输入用户名">'+
                                '<button id="search" class="mt-3 float-end btn btn-secondary btn-sm" onclick="search();">搜索好友</button>'+
                           '</p>'+
                            '<p>'+
                                '<input id="user_id" type="number" placeholder="用户ID" name="" id="">'+
                                '<input id="message" class="ms-2" type="text" placeholder="不打也阔以" name="" id="">'+
                                '<button id="submit" class="btn btn-secondary btn-sm float-end" onclick="add_friend();">发送申请</button>'+
                            '</p>'+
                            '<p class="mt-2 border-bottom border-4"></p>'+
                            '<table class="table">'+
                            '<thead>'+
                              '<tr>'+
                                '<th scope="col">ID</th>'+
                                '<th scope="col">用户名</th>'+
                                '<th scope="col">学院</th>'+
                                '<th scope="col">专业</th>'+
                                '<th scope="col">性别</th>'+
                                '<th scope="col">年龄</th>'+
                                '<th scope="col">个人简介</th>'+
                              '</tr>'+
                            '</thead>'+
                            '<tbody id="tbody">'+
                            '</tbody>'+
                          '</table>'

                $("#content").html(str_s);                
            }
        });
    });



    // 编辑信息
    $("#edit-user-info").click(function () {
        window.location.href = "/edit-user-info/";
    });

});


// 删除帖子
function deletet(ths) {
    var tid = $(ths).attr("iddd");
    $.ajax({
        url: "/manage-blog/",
        type: "POST",
        data: {
            'type': 'delete',
            't_id': tid
        },
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            // data是服务器端返回的字符串
            if (!data.status) alert(data.msg);
            else window.location.href="/user-info/";
        }
    });
};
// 搜索用户
function search() {
    var username = $("#username").val();
    $.ajax({
        url: "/add-friend?other_username="+username,
        type: "GET",
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            friends = data.friends
            var str = '';
            for(var i=0;i<friends.length;i++){
                str = '<tr>'+          
                '<th scope="row">'+ friends[i].id +'</th>'+
                '<td>'+ friends[i].username +'</td>'+
                '<td>'+ friends[i].faculty +'</td>'+
                '<td>'+ friends[i].major +'</td>'+
                '<td>'+ friends[i].sex +'</td>'+
                '<td>'+ friends[i].age +'</td>'+
                '<td>'+ friends[i].profile +'</td>'+
            '</tr>';
            }
            $("#tbody").html(str);
            
        }
    });
   
};

// 添加好友
function add_friend() {
    var message = $("#message").val();
    var user_id = $("#user_id").val();
    console.log(message, user_id)
    $.ajax({
        url: "/add-friend/",
        type: "POST",
        data: {
            'other_id': user_id,
            'message': message
        },
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            console.log("yes2")
            // data是服务器端返回的字符串
            if (!data.status) alert(data.msg);
            else window.location.href="/user-info/";
        }
    });
};

// 同意添加好友
function agree(ths) {
    var other_id = $(ths).attr("other_id");
    $.ajax({
        url: "/get-request/",
        type: "POST",
        data: {
            'type': 'agree',
            'other_id': other_id
        },
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            // data是服务器端返回的字符串
            if (!data.status) alert(data.msg);
            else window.location.href="/user-info/";
        }
    });
};

// 拒绝添加好友
function refusal(ths) {
    var other_id = $(ths).attr("other_id");
    $.ajax({
        url: "/get-request/",
        type: "POST",
        data: {
            'type': 'refusal',
            'other_id': other_id
        },
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            // data是服务器端返回的字符串
            if (!data.status) alert(data.msg);
            else window.location.href="/user-info/";
        }
    });
};


// 拒绝添加好友
function deletet_friend(ths) {
    var other_id = $(ths).attr("other_id");
    $.ajax({
        url: "/my-friends/",
        type: "POST",
        data: {
            'other_id': other_id
        },
        beforeSend: function (xhr, settings) {
            xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");    // csrf
        },
        success: function (data) {
            // data是服务器端返回的字符串
            if (!data.status) alert(data.msg);
            else window.location.href="/user-info/";
        }
    });
};
